﻿using System.Linq;
using Coder.Workflow;
using Fee.Mapping;
using Fee.Mapping.FileAttachs;
using Fee.Mapping.Orders;
using Fee.Mapping.Orders.AuthProjectOrders;
using Fee.Mapping.Orders.BudgetOrders;
using Fee.Mapping.Orders.FormalOrders;
using Fee.Mapping.Orders.PreDesignOrder;
using Fee.Orders;
using Fee.Orders.AuthProjectWorkflow;
using Fee.Orders.DesignWorkflow;
using Fee.Orders.FormalWorkflow;
using Fee.Orders.PreWorkflow;
using Fee.VendorMatchers;
using Identity;
using Identity.Mapping;
using Microsoft.EntityFrameworkCore;

namespace Fee
{
    public abstract class FeeDbContext : WorkflowDbContext
    {
        protected FeeDbContext(DbContextOptions options) : base(options, typeof(FeeWorkflowNumberGenerator))
        {
        }


        public IQueryable<Project> Projects => Set<Project>();

        public IQueryable<FrameworkContract> FrameworkContracts => Set<FrameworkContract>();
        public IQueryable<OrderContract> OrderContracts => Set<OrderContract>();
        public IQueryable<Station> Stations => Set<Station>();

        public IQueryable<VendorMatcher> VendorMatchers => Set<VendorMatcher>();

        public IQueryable<DelegateOrderDetail> DelegateOrderDetails => Set<DelegateOrderDetail>();
        public IQueryable<BudgetOrder> DelegateOrders => Set<BudgetOrder>();
        public IQueryable<PreDelegateOrder> PreDelegateOrders => Set<PreDelegateOrder>();
        public IQueryable<ConstructionTask> ConstructionTasks => Set<ConstructionTask>();
        public IQueryable<ConstructionDetail> DelegateTaskDetails => Set<ConstructionDetail>();

        public IQueryable<AuthProjectWorkProcess> AuthProjectWorkProcesses => Set<AuthProjectWorkProcess>();
        public IQueryable<BudgetOrderWorkProcess> BudgetOrderWorkProcess => Set<BudgetOrderWorkProcess>();
        public IQueryable<PreDelegateWorkProcess> PreDelegateWorkProcesses => Set<PreDelegateWorkProcess>();

        public IQueryable<FormalOrderWorkProcess> FormalOrderWorkProcesses => Set<FormalOrderWorkProcess>();
        //  public IQueryable<FrameworkContractProject> FrameworkContractProjects => Set<FrameworkContractProject>();


        protected override void OnModelCreating(ModelBuilder builder)
        {
            //基础信息
            builder.ApplyConfiguration(new ProjectMapping());
            builder.ApplyConfiguration(new ProjectSettingMapping());

            builder.ApplyConfiguration(new FrameworkContractMapping());
            builder.ApplyConfiguration(new OrderContractMapping());
            builder.ApplyConfiguration(new DistrictMapping());
            builder.ApplyConfiguration(new StationMapping());

            builder.ApplyConfiguration(new VendorMatcherMapping());
            //builder.Ignore<VendorMatcher>();
            // builder.ApplyConfiguration(new FrameworkContractProjectMapping());
            builder.ApplyConfiguration(new ImpleVendorMatcherMapping());
            builder.ApplyConfiguration(new DesignVendorMatcherMapping());
            builder.ApplyConfiguration(new SupervisorVendorMatcherMapping());

            builder.ApplyConfiguration(new ConstructionTaskCostDetailMapping());
            builder.ApplyConfiguration(new ConstructionTaskPlanCostMapping());

            builder.ApplyConfiguration(new BudgetOrderPlanCostMapping());
            //file attachments
            builder.ApplyConfiguration(new FileAttachMapping());

            //设计模块


            //工单信息

            builder.ApplyConfiguration(new ConstructionTaskMapping());
            builder.ApplyConfiguration(new ConstructionTaskDetailMapping());
            builder.ApplyConfiguration(new ConstructionTaskFileAttachMapping());

            //设计 预算，图纸
            builder.ApplyConfiguration(new BudgetOrderMapping());
            builder.ApplyConfiguration(new BudgetOrderWorkTaskMapping());
            builder.ApplyConfiguration(new DelegateOrderDetailMapping());
            builder.ApplyConfiguration(new BaseOrder());
            builder.ApplyConfiguration(new BudgetOrderWorkflowMapping());
            builder.ApplyConfiguration(new BudgetFileAttachOrderFileAttachMapping());

            //预委托
            builder.ApplyConfiguration(new PreDelegateOrderProcessMapping());
            builder.ApplyConfiguration(new PreDelegateWorkTaskMapping());
            builder.ApplyConfiguration(new PreDelegateOrderMapping());

            builder.ApplyConfiguration(new ProjectNumberMappingMapping());
            //正式委托
            builder.ApplyConfiguration(new FormalOrderMapping());
            builder.ApplyConfiguration(new FormalOrderWorkflowMapping());
            builder.ApplyConfiguration(new FormalOrderWorkTaskMapping());
            builder.ApplyConfiguration(new FormalOrderPlanCostMapping());

            //完工会审
            builder.ApplyConfiguration(new AuthProjectOrderMapping());
            builder.ApplyConfiguration(new AuthProjectOrderWorkflowMapping());
            builder.ApplyConfiguration(new AuthProjectOrderWorkTaskMapping());
            builder.ApplyConfiguration(new AuthProjectOrderMapping());
            builder.ApplyConfiguration(new AuthProjectOrderFileAttachMapping());
            builder.ApplyConfiguration(new StationCostDetailMapping());
            builder.ApplyConfiguration(new DelegateOrderCostDetailMapping());
            builder.ApplyConfiguration(new AuthProjectOrderCostDetailMapping());

            //builder.Ignore(typeof(WorkProcess));
            //必须在方法最后一个地方，否则会出现问题。
            base.OnModelCreating(builder);
        }
    }
}